﻿using DIM_CONFIG;
using DIM_From;
using FontAwesome.Sharp;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Color = System.Drawing.Color;

namespace DIM_USER
{
    public partial class Index : Form
    {
        private IconButton currentButton;
        private Random random;
        private int tempIndex;
        private Form activeForm;
        public Index()
        {
            InitializeComponent();
            random = new Random();
            this.Text = string.Empty;
            this.ControlBox = false;
            ThemeColor.PrimaryColor = panel_title.BackColor;
            ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(panel_title.BackColor, -0.3);
        }
        [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        [DllImport("user32.DLL", EntryPoint = "SendMessage")]
        private extern static void SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
        private System.Drawing.Color SelectThemeColor()
        {
            int index = random.Next(ThemeColor.colorList.Count);
            while (tempIndex == index)
            {
                index = random.Next(ThemeColor.colorList.Count);
            }
            tempIndex = index;
            string color = ThemeColor.colorList[index];
            return ColorTranslator.FromHtml(color);
        }
        private void ActivateButton(object btnSender)
        {
            if (btnSender != null)
            {
                if (currentButton != (IconButton)btnSender)
                {
                    DisableButton();
                    System.Drawing.Color color = SelectThemeColor();
                    currentButton = (IconButton)btnSender;
                    currentButton.BackColor = color;
                    currentButton.ForeColor = Color.White;
                    currentButton.Font = new System.Drawing.Font("宋体", 13F);
                    panel_title.BackColor = color;
                    panel_logo.BackColor = ThemeColor.ChangeColorBrightness(color, -0.3);
                    ThemeColor.PrimaryColor = color;
                    ThemeColor.SecondaryColor = ThemeColor.ChangeColorBrightness(color, -0.3);
                }
            }
        }
        private void DisableButton()
        {
            foreach (Control previousBtn in panel_menu.Controls)
            {
                if (previousBtn.GetType() == typeof(IconButton))
                {
                    previousBtn.BackColor = Color.FromArgb(51, 51, 76);
                    previousBtn.ForeColor = Color.Gainsboro;
                    previousBtn.Font = new System.Drawing.Font("宋体", 10F);
                }
            }
        }
        private void OpenChildForm(Form chileForm, object btnSender)
        {
            if (activeForm != null)
            {
                activeForm.Close();
            }
            ActivateButton(btnSender);
            activeForm = chileForm;
            chileForm.TopLevel = false;
            chileForm.FormBorderStyle = FormBorderStyle.None;
            chileForm.Dock = DockStyle.Fill;
            this.panel_body.Controls.Add(chileForm);
            chileForm.BringToFront();
            chileForm.Show();
            label_title.Text = chileForm.Text;
        }

        private void iconButton_Click(object sender, EventArgs e)
        {
            IconButton button = (IconButton)sender;
            switch (button.Name.ToString())
            {
                case "Bold":
                    {
                        OpenChildForm(new Bold_form(), sender);
                        break;
                    }
                case "CartShopping":
                    {
                        OpenChildForm(new CartShopping_form(), sender);
                        break;
                    }
                case "Folder":
                    {
                        OpenChildForm(new Folder_form(), sender);
                        break;
                    }
                case "Ghost":
                    {
                        OpenChildForm(new Ghost_form(), sender);
                        break;
                    }
                case "Globe":
                    {
                        OpenChildForm(new Globe_form(), sender);
                        break;
                    }
                case "Handshake":
                    {
                        OpenChildForm(new Handshake_form(), sender);
                        break;
                    }
                case "Snowflake":
                    {
                        OpenChildForm(new Snowflake_form(), sender);
                        break;
                    }
            }
        }

        private void panel_title_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }

        private void Exit_form_Click(object sender, EventArgs e)
        {
            if (new Msgform(ThemeColor.PrimaryColor, new Tuple<string, string>("温馨提示", "是否要关闭程序")).ShowDialog() == DialogResult.OK)
            {
                this.Dispose();
                this.Close();
            }
        }

        private void Max_form_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }

        private void Hide_form_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void 奇幻维度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }
    }
}